package org.eclipseuseradmin.internal.demo.wizards;

import org.eclipse.core.databinding.Binding;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.beans.PojoObservables;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.wizard.WizardPageSupport;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipseuseradmin.internal.demo.model.CustomerModel;
import org.eclipseuseradmin.ui.model.RoleElementModel;
import org.eclipseuseradmin.ui.wizards.BaseNewRoleElementWizard;

public class AddressWizardPage extends WizardPage {

	private DataBindingContext bindingContext;

	protected Composite mainComposite;

	protected Label addressLabel;

	protected Text addressText;

	public AddressWizardPage() {

		super("address", "Address", null);
		setDescription("Enter your address");
		bindingContext = new DataBindingContext();
		WizardPageSupport.create(this, bindingContext);
	}

	public void createControl(Composite parent) {

		mainComposite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		mainComposite.setLayout(layout);

		addressLabel = new Label(mainComposite, SWT.NONE);
		addressLabel.setText("Address:");

		addressText = new Text(mainComposite, SWT.BORDER);
		GridData layoutData = new GridData(GridData.FILL_HORIZONTAL);
		addressText.setLayoutData(layoutData);
		bindNameValue();

		setControl(mainComposite);
	}

	protected void bindNameValue() {

		final CustomerModel model = (CustomerModel) getNewRoleElementWizard().getModel();

		UpdateValueStrategy updateValueStrategy = new UpdateValueStrategy();
		Binding binding = bindingContext.bindValue(SWTObservables.observeText(
				addressText, SWT.FocusOut), PojoObservables.observeValue(model,
				"address"), updateValueStrategy, null);
	}

	protected BaseNewRoleElementWizard getNewRoleElementWizard() {
		return ((BaseNewRoleElementWizard) getWizard());
	}

	protected DataBindingContext getBindingContext() {
		return bindingContext;
	}

}
